package com.fh.service.hydropower;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.entity.system.TreeNode;
import com.fh.util.PageData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service("hydropowerSettingService")
public class HydropowerSettingService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    /*
    * 新增
    */
    public void save(PageData pd) throws Exception {
        dao.save("HydropowerSettingMapper.save", pd);
    }

    /*
    * 新增初始读数
    */
    public void saveInitRead(PageData pd) throws Exception {
        dao.save("HydropowerSettingMapper.saveInitRead", pd);
    }

    /*
    * 新增关联关系
    */
    public void saveGlgx(PageData pd) throws Exception {
        dao.save("HydropowerSettingMapper.saveGlgx", pd);
    }

    /*
    * 删除
    */
    public void delete(PageData pd) throws Exception {
        dao.delete("HydropowerSettingMapper.delete", pd);
    }

    /*
    * 删除子表
    */
    public void deleteZB(PageData pd) throws Exception {
        dao.delete("HydropowerSettingMapper.deleteZB", pd);
    }

    /*
    * 删除关联关系
    */
    public void deleteGlgx(PageData pd) throws Exception {
        dao.delete("HydropowerSettingMapper.deleteGlgx", pd);
    }

    /*
    * 修改
    */
    public void edit(PageData pd) throws Exception {
        dao.update("HydropowerSettingMapper.edit", pd);
    }

    /*
     * 修改水电表默认抄表日
     */
    public void edit_read_day(PageData pd) throws Exception {
        dao.update("HydropowerSettingMapper.edit_read_day", pd);
    }

    /*
     * 通过id获取数据
     */
    public PageData init_read_day(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerSettingMapper.init_read_day", pd);
    }

    /*
     *列表
     */
    public List<PageData> list(Page page) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerSettingMapper.datalistPage", page);
    }

    /*
     * 通过building_id获取默认价格
     */
    public PageData findPriceByBid(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerSettingMapper.findPriceByBid", pd);
    }

    /*
     * 新增默认价格
     */
    public void savePrice(PageData pd) throws Exception {
        dao.save("HydropowerSettingMapper.savePrice", pd);
    }

    /*
     * 修改默认价格
     */
    public void editPrice(PageData pd) throws Exception {
        dao.update("HydropowerSettingMapper.editPrice", pd);
    }



    /*
    * 修改
    */
    public void updateIsNew(PageData pd) throws Exception {
        dao.update("HydropowerSettingMapper.updateIsNew", pd);
    }

    /*
    *列表(全部)
    */
    public List<PageData> listAll(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerSettingMapper.listAll", pd);
    }

    /*
    * 通过id获取数据
    */
    public PageData findInfoById(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerSettingMapper.findInfoById", pd);
    }

    /*
    * 通过id获取数据
    */
    public PageData findById(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerSettingMapper.findById", pd);
    }

    /*
    * 批量删除
    */
    public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
        dao.delete("HydropowerSettingMapper.deleteAll", ArrayDATA_IDS);
    }

    /*
    * 通过PID获取水电表信息
     */
    public List<PageData> findByPid(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerSettingMapper.findByPid", pd);
    }


    /*
    tree
     */
    public TreeNode recursiveTree(String id, PageData pd) throws Exception {
        TreeNode node = new TreeNode();
        pd.put("id", id);
        PageData pnode = findById(pd);
//        String remarks = StringHelper.isNotEmpty(pnode.getString("REMARKS"))?"("+pnode.getString("REMARKS")+")":"";
        node.setId(pnode.getString("ID"));
        node.setName(pnode.getString("CODE"));
        node.setPid(pnode.getString("PID"));
        pd.put("pid", id);
        List<PageData> childs = findByPid(pd);
        for (PageData child : childs) {
            TreeNode c = recursiveTree(child.getString("ID"), pd);
            node.getChildren().add(c);
        }
        return node;
    }


    /*
    * 通过PID获取水电表信息
     */
    public PageData findByIdFor(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerSettingMapper.findByIdFor", pd);
    }

    /*
* 修改时根据ID获取数据
 */
    public PageData findByIdForEdit(PageData pd) throws Exception {
        return (PageData) dao.findForObject("HydropowerSettingMapper.findByIdForEdit", pd);
    }

    public void editList(List<PageData> list) throws Exception{
        dao.update("HydropowerSettingMapper.editList",list);
    }

    public JSONArray getArray(List<PageData> floors,List<PageData> types){
        JSONArray arr = new JSONArray();
        for(PageData pd:floors){
            JSONObject object = new JSONObject();
            object.put("id",pd.getString("FLOOR_ID"));
            object.put("name",pd.get("FLOOR_NAME")+"层");
            arr.add(object);
        }
        for (PageData pd2:types){
            if(!pd2.getString("ID").equals("ee96f524afbb49caa94d6342983d91a0")){
                JSONObject object2 = new JSONObject();
                object2.put("id",pd2.getString("ID"));
                object2.put("name",pd2.getString("NAME"));
                arr.add(object2);
            }
        }
        return arr;
    }

    public List<PageData> findProject(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerSettingMapper.findProject", pd);
    }

    public PageData findForChange(PageData pd)throws Exception{
        return (PageData) dao.findForObject("HydropowerSettingMapper.findForChange",pd);
    }

    public List<PageData> findTreeTable(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("HydropowerSettingMapper.findTreeTable", pd);
    }
}

